package dao

import (
	"context"
	"errors"

	"bilinadmin/internal/model"
	"bilinadmin/pkg/db/pgex"
)

// BrandDao brand dao
type BrandDao interface {
	// CreateBrand 新建品牌
	CreateBrand(ctx context.Context, brand *model.Brand) (int64, error)
	// GetBrandByIds 根据ids获取品牌
	GetBrandByIds(ctx context.Context, ids []int64) ([]*model.Brand, error)
	// GetBrandById 根据id获取品牌
	GetBrandById(ctx context.Context, id int64) (*model.Brand, error)
	// UpdateBrandInfo 更新品牌信息
	UpdateBrandInfo(ctx context.Context, brand *model.Brand) error
	// UpdateBrandSuEmp 更新品牌超级管理员
	UpdateBrandSuEmp(ctx context.Context, brand *model.Brand) error
}

var brandDao BrandDao

type bDaoImpl struct {
	*DaoBase
	name string // 表名
}

// InitBrandDao 初始化 brand dao
func InitBrandDao() {
	brandDao = &bDaoImpl{
		DaoBase: daoBase,
		name:    "brand",
	}
}

// GetBrandDao get brand dao
func GetBrandDao() BrandDao {
	return brandDao
}

// CreateBrand 新建品牌
func (dao *bDaoImpl) CreateBrand(ctx context.Context, brand *model.Brand) (int64, error) {
	if brand == nil {
		return 0, nil
	}
	param := []interface{}{
		brand.Name,
		brand.Logo,
		brand.Description,
		brand.SuEmpId,
		brand.BizType,
		brand.AppScope,
		brand.Status,
		brand.CreatorId,
		brand.CreatedAt,
		brand.UpdatedAt,
	}
	query := "INSERT INTO brand (name, logo, description, su_emp_id, biz_type, app_scope, status, creator_id, " +
		"created_at, updated_at) VALUES (?,?,?,?,?,?,?,?,?,?)"

	aff, err := dao.db.PgExec(ctx, "id", query, param...)
	if err != nil {
		return 0, err
	}
	lastId, err := aff.GetLastId()
	if err != nil {
		return 0, err
	}
	return lastId, nil
}

// GetBrandByIds 根据id获取品牌
func (dao *bDaoImpl) GetBrandByIds(ctx context.Context, ids []int64) ([]*model.Brand, error) {
	query := "SELECT * FROM brand WHERE id IN ? AND deleted_at=0"
	var brands []*model.Brand
	err := dao.db.Query(ctx, &brands, query, ids)
	return brands, err
}

// UpdateBrandInfo 更新品牌信息
func (dao *bDaoImpl) UpdateBrandInfo(ctx context.Context, brand *model.Brand) error {
	if brand == nil {
		return nil
	}
	param := []interface{}{
		brand.Name,
		brand.Logo,
		brand.Description,
		brand.BizType,
		brand.SuEmpId,
		brand.AppScope,
		brand.UpdatedAt,
		brand.Id,
	}
	query := "UPDATE brand SET name=?, logo=?, description=?, biz_type=?, su_emp_id=?, app_scope=?, updated_at=? " +
		"WHERE id=? AND deleted_at=0"
	_, err := dao.db.Update(ctx, query, param...)
	return err
}

// UpdateBrandSuEmp 更新品牌超级管理员
func (dao *bDaoImpl) UpdateBrandSuEmp(ctx context.Context, brand *model.Brand) error {
	if brand == nil || brand.Id == 0 || brand.SuEmpId == 0 {
		return nil
	}
	query := "UPDATE brand SET su_emp_id=?, updated_at=? WHERE id=? AND deleted_at=0"
	_, err := dao.db.Update(ctx, query, brand.SuEmpId, brand.UpdatedAt, brand.Id)
	return err
}

// GetBrandById 根据id获取品牌
func (dao *bDaoImpl) GetBrandById(ctx context.Context, id int64) (*model.Brand, error) {
	query := "SELECT * FROM brand WHERE id=? AND deleted_at=0"
	brand := &model.Brand{}
	err := dao.db.Get(ctx, brand, query, id)
	if errors.Is(err, pgex.ErrEmpty) {
		return nil, nil
	}
	return brand, err
}
